﻿using System;
using System.Drawing;
public partial class ImgCode : System.Web.UI.Page
{
    private const string Codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private const int ImgWidth = 48;
    private const int ImgHeight = 18;
    protected void Page_Load(object sender, EventArgs e)
    {
        string code = "";
        Bitmap img = new Bitmap(ImgWidth, ImgHeight);
        Graphics g = Graphics.FromImage(img);
        SolidBrush brush = new SolidBrush(Color.FromArgb(255, 0xEE, 0x33, 0x88));
        Font font = new Font("Arial", 11, FontStyle.Bold);
        //Font font = new Font("Kingsoft Phonetic Plain", 11, FontStyle.Bold);
        //Font font = new Font("Kingsoft Phonetic Plain", 12);
        Pen pen = new Pen(brush, 1);
        int i, j;
        Random rnd = new Random();
        for (i = 0; i < 4; i++)
        {
            code = code + Codes.Substring(rnd.Next() % 26, 1);
        }
        g.Clear(Color.White);
        for (i = 0; i < ImgWidth; i += 1)
        {
            for (j = 0; j < ImgHeight; j += 1)
            {
                if (rnd.Next() % 30 == 0)
                {
                    g.DrawEllipse(pen, i, j, 1, 1);
                }
            }
        }
        g.DrawString(code, font, brush, 0, 0);
        //g.DrawString("/ 7intE5mi:djEt /", font, brush, 0, 0);
        Session["ImgCode"] = code;
        Response.ContentType = "image/gif";
        img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
        img.Dispose();
        g.Dispose();
    }
}
